package com.slm.krstartimes.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.slm.krstartimes.R;
import com.slm.krstartimes.utils.L;
import com.slm.krstartimes.utils.MyImageLoader;
import com.slm.krstartimes.utils.News;

import java.util.List;

/**
 * Created by dllo on 15/8/17.
 */
public class CollectAdapter extends BaseAdapter {
    private Context context;
    private List<News> data;
    private ImageLoader imageLoader;
    private RequestQueue requestQueue;
    public CollectAdapter(Context context, List<News> data) {
        this.context = context;
        this.data = data;
        requestQueue = Volley.newRequestQueue(context.getApplicationContext());
        imageLoader = new ImageLoader(requestQueue, new MyImageLoader());
    }

    @Override
    public int getCount() {
        if (data != null && data.size() > 0) {
            return data.size();
        }
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        if (data != null && data.size() > 0) {
            return data.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            view = inflater.inflate(R.layout.collect_list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.title= (TextView) view.findViewById(R.id.title);
            viewHolder.postDate= (TextView) view.findViewById(R.id.postDate);
            viewHolder.title_picture= (ImageView) view.findViewById(R.id.title_picture);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if (data != null && data.size() > 0) {
            News news = data.get(i);
            if (news.getImage().length() == 65) {// 判断获取到的图片地址是否符合要求,为避免出现地址失效,采用地址拼接的方法
                String newImgUrl = "http://img.ithome.com/newsuploadfiles/thumbnail" + news.getImage().substring(news.getImage().length() - 18, news.getImage().length());
                ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(viewHolder.title_picture,
                        R.mipmap.load, R.mipmap.load);// 参数说明:图片地址,默认图片,加载失败显示的图片
                imageLoader.get(newImgUrl, imageListener);//加载图片
            } else if (news.getImage().length() == 66) {
                String newImgUrl = "http://img.ithome.com/newsuploadfiles/thumbnail" + news.getImage().substring(news.getImage().length() - 19, news.getImage().length());
                ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(viewHolder.title_picture,
                        R.mipmap.load, R.mipmap.load);
                imageLoader.get(newImgUrl, imageListener);
            } else {
                viewHolder.title_picture.setImageResource(R.mipmap.appic);
                L.i("网址无效" + news.getImage());
            }
            viewHolder.title.setText(news.getTitle());
            viewHolder.postDate.setText(news.getCommentcount());
        }
        return view;
    }

    public class ViewHolder {
        ImageView title_picture;
        TextView title,postDate;
    }
}
